﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
public class cartIDs
{
    public int cartId;
    public int count;
}
public partial class Cart_selAdd : OTS.MemberPage
{
    private OTS.Model.custom custom;
    protected string strConent;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.TestLogin(HttpContext.Current))
        {
            OTS.Common.Jscript.AlertAndRedirect("你还没有登陆！", "login.aspx");
            return;
        }
        custom = this.GetCustom(HttpContext.Current);
        if (!Page.IsPostBack)
        {
            PageBind();
        }
        


    }
    private void PageBind()
    {
        string strCardIds = Request.QueryString["cartIds"];
        if (strCardIds == null || strCardIds == "")
        {
            OTS.Common.Jscript.AlertAndRedirect("请选择要购买的商品", "cart.aspx");
            return;
        }
        string[] arrCard = strCardIds.Split(',');
        string[] arrCardCount;
        List<cartIDs> lst = new List<cartIDs>();
        string strIDs = "";
        for (int i = 0; i < arrCard.Length; i++)
        {
            arrCardCount = arrCard[i].Split('-');
            if (arrCardCount.Length != 2)
            {
                OTS.Common.Jscript.AlertAndRedirect("参数错误,请重新选择商品！", "cart.aspx");
                return;
            }
            else
            {
                if (OTS.Common.Help.IsNumber(arrCardCount[0]) && OTS.Common.Help.IsNumber(arrCardCount[1]))
                {
                    cartIDs model = new cartIDs();
                    model.cartId = int.Parse(arrCardCount[0]);
                    model.count = int.Parse(arrCardCount[1]);
                    if (model.cartId < 1 || model.count < 1)
                    {
                        OTS.Common.Jscript.AlertAndRedirect("参数错误,请重新选择商品！", "cart.aspx");
                        return;
                    }
                    else
                    {
                        lst.Add(model);
                        strIDs += arrCardCount[0] + ",";
                    }
                }
                else
                {
                    OTS.Common.Jscript.AlertAndRedirect("参数错误,请重新选择商品！", "cart.aspx");
                    return;
                }
            }
        }
        if (strIDs != "")
        {
            strIDs = strIDs.Substring(0, strIDs.Length - 1);
        }
        else
        {
            OTS.Common.Jscript.AlertAndRedirect("参数错误,请重新选择商品！", "cart.aspx");
            return;
        }

        OTS.BLL.CartBLL bll = new OTS.BLL.CartBLL();
        DataTable dt = bll.GetCartsByIds(strIDs, custom.cusID);
        if (dt.Rows.Count != lst.Count)
        {
            OTS.Common.Jscript.AlertAndRedirect("参数错误,请重新选择商品！", "cart.aspx");
            return;
        }
        ViewState["CartIDs"] = strCardIds;
        StringBuilder str = new StringBuilder();
        int count=1, price=0, total = 0;
        foreach (DataRow dr in dt.Rows)
        {
            price = int.Parse(dr["retailPrice"].ToString());
            for (int i = 0; i < lst.Count; i++)
            { 
                if(lst[i].cartId == int.Parse(dr["cartID"].ToString()))
                {
                    count = lst[i].count;
                    break;
                }
            }

            str.Append("<tr cartID=\"" + dr["cartID"].ToString() + "\">"); 
            str.Append(" <td>");
            str.Append("    <a style=\"border: 0 none;\" target=\"_blank\" href=\"product.aspx?proID=" + dr["proID"].ToString() + " \">");
            if (dr["bigImg_pic"].ToString()=="")
            {
                str.Append("<img src='img/yes_picture.gif' />");
            }
            else
            {
                str.Append("<img src='" + PictureProcessing.ImgPathToThumbURL(dr["bigImg_pic"].ToString()) + "' />");
            }
            str.Append("</a>");
            str.Append(" </td>");
            str.Append(" <td style=\"text-align: left;\" >");
            str.Append("    <a style=\"border: 0 none;\" target=\"_blank\" href=\"product.aspx?proID=" + dr["proID"].ToString() + " \">");
            str.Append(dr["title"].ToString());
            str.Append("    </a>");
            str.Append(" </td>");
            str.Append(" <td>");
            str.Append(dr["sizeName"].ToString());
            str.Append(" </td>");
            str.Append(" <td>");
            str.Append(dr["col_color"].ToString());
            str.Append(" </td>");
            str.Append(" <td class=\"mktprice1\">");
            str.Append(dr["wholeSalePrice"].ToString());
            str.Append(" </td>");
            str.Append(" <td>");
            str.Append(dr["retailPrice"].ToString());
            str.Append(" </td>");
            str.Append(" <td ><div class=\"Numinput\">");
            str.Append(count);
            str.Append("  </div>");
            str.Append(" </td>");
            str.Append(" <td class=\"itemTotal fontcolorRed\">");
            str.Append((count * price));
            str.Append(" </td>"); 
            str.Append("</tr>");
            total += (count * price);
        }
        this.lblTtotal.Text = total.ToString();
        strConent = str.ToString();

        //获取地址
        OTS.BLL.DeliverAddressBLL bllAddress = new OTS.BLL.DeliverAddressBLL();
        this.rtpAdd.DataSource = bllAddress.GetCustomAddress(custom.cusID);
        this.rtpAdd.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string strAdd = "", strConsignee = "", strPhone = "", strPostCode = "";
        if (ViewState["CartIDs"] == null || ViewState["CartIDs"].ToString()=="")
        {
            OTS.Common.Jscript.AlertAndRedirect("未选择商品！", "cart.aspx");
            return;
        }
        if (this.txtAddNew.Text == "1")
        {
            OTS.Model.deliverAddress model = new OTS.Model.deliverAddress();
            model.address = this.txtAddress.Text;

            model.area = this.txtArea.Text;
            model.city = this.txtCity.Text;
            model.consignee = this.txtName.Text;
            model.cusID = custom.cusID;
            model.isDefault =   0;
            model.phone = this.txtPhone.Text;
            model.phone1 = this.txtPhone1.Text;
            model.postCode = this.txtPostCode.Text;
            model.province = this.txtProvince.Text;
            OTS.BLL.DeliverAddressBLL bll = new OTS.BLL.DeliverAddressBLL();
            bll.Add(model);
            strAdd = model.province + " " + model.city + " " + model.area + " " + model.address;
            strConsignee = model.consignee;
            strPhone = model.phone + " " + model.phone1;
            strPostCode = model.postCode;
        }
        else if (this.txtAddNew.Text == "0")
        {
            strAdd = this.txtDeliverAddress.Text;
            strConsignee = this.txtDeliverName.Text;
            strPhone = this.txtDeliverPhone.Text;
            strPostCode = this.txtDeiverPostCode.Text;
        }
        OTS.BLL.SaleOrderBLL bllOrder = new OTS.BLL.SaleOrderBLL();
        OTS.Model.saleOrder modelSale = new OTS.Model.saleOrder();
        modelSale.address = strAdd;
        modelSale.consignee = strConsignee;
        modelSale.phone = strPhone;
        modelSale.postcode = strPostCode;
        modelSale.cusID = custom.cusID;
        modelSale.orderNO = bllOrder.GetUniqueOrderNO();

        int retID = bllOrder.CartToSaleOrder(modelSale, ViewState["CartIDs"].ToString());
        if (retID > 0)
        {
             Response.Redirect("Order-Pay.aspx?orderNO=" + modelSale.orderNO + "&orderId=" + retID.ToString());
            return;
        }
        else
        {
            OTS.Common.Jscript.Alert("参数错误！");
            return;
        }
    }
}
